<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../../../component/pear/css/pear.css"/>
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">教师工号</label>
                    <div class="layui-input-block">
                        <input id="teacherId" type="text"
                               lay-verify="required" name="teacherId" value="" lay-verify="title"
                               autocomplete="off" lay-reqText="请输入账号"
                               placeholder="请输入教师工号" class="layui-input">
                        <span class="layui-badge"
                              id="userIdTips" style="text-align: center;display: none">该教师已存在
                        </span>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">姓名</label>
                    <div class="layui-input-block">
                        <input id="realName" type="text" name="realName" lay-verify="required" autocomplete="off"
                               placeholder="请输入姓名"
                               class="layui-input" lay-reqText="请输入姓名">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">职工职位</label>
                    <div class="layui-input-block">
                        <select id="identityName" name="identityName"  lay-verify="required"
                                lay-reqText="请选择该教师的职工职位" lay-search>

                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">电话</label>
                    <div class="layui-input-block">
                        <input id="phone" type="text" name="phone" value="" lay-verify="required|phone"
                               autocomplete="off"
                               placeholder="请输入手机号码" class="layui-input" lay-reqText="请输入手机号码">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">家庭住址</label>
                    <div class="layui-input-block">
                        <input id="addr" type="text" name="addr" value="" lay-verify="title"
                               autocomplete="off" placeholder="请输入家庭地址" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">出生日期</label>
                    <div class="layui-input-block">
                        <input id="birthday" type="text" name="birthday" value="" lay-verify="required"
                               autocomplete="off"
                               placeholder="请输入出生日期" class="layui-input" lay-reqText="请输入出生日期">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">性别</label>
                    <div class="layui-input-block">
                        <input id="man" type="radio" name="sex" value="1" title="男">
                        <input id="woman" type="radio" name="sex" value="2" title="女" checked>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script src="../../../component/layui/layui.js"></script>
<script src="../../../component/pear/pear.js"></script>
<script>
    layui.use(['form', 'jquery', 'loading','laydate'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let loading = layui.loading;
        let laydate = layui.laydate;

        laydate.render({
            elem: '#birthday' //或 elem: document.getElementById('test')、elem: lay('#test') 等
            ,theme: 'molv'
            ,calendar: true
        });

        //动态获取专业列表
        $.ajax({
            url: '/Identity/getAllIdentity',
            dataType: 'json',
            type: 'post',
            success: function (res) {
                console.log(res.data);
                let identity = $('#identityName');
                identity.append('<option value="">请选择专业</option>')
                for (let i = 0; i < res.data.length; i++) {
                    let identityName = res.data[i].identity;
                    identity.append('<option value="' + identityName + '">' + identityName + '</option>')
                }
                form.render('select');
            }
        })

        form.on('submit(user-save)', function (data) {
            //console.log(data.field);
            loading.Load(3, "");
            $.ajax({
                url: '/admin/tea/addTea',
                data: data.field,
                dataType: 'json',
                type: 'post',
                success: function (result) {
                    loading.loadRemove();
                    console.log(result);
                    if (result.success=='1') {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            //console.log(JSON.stringify(result.data));
                            parent.layui.table.reload("user-table", {
                                data: result.data,
                                page: {
                                    layout: ['count', 'prev', 'page', 'next', 'skip']
                                    , curr: parent.layui.jquery(".layui-laypage-em").next().html()
                                    , groups: 3
                                    , last: false
                                }
                            });
                        });
                    } else {
                        layer.open({
                            title: '提示'
                            , icon: 2
                            , content: result.msg
                        });
                    }
                }
            })
            return false;
        });
        //前端实时检测用户是否存在
        $('#teacherId').blur(function () {
            if ($('#teacherId').val() == '') {
                return false;
            }
            $('#userIdTips').append('<i class="tipsLoad layui-icon layui-icon-loading layui-anim layui-anim-rotate layui-anim-loop"></i>');
            $.ajax({
                url: '/admin/tea/searchTea',
                data: {
                    teacherId: $('#teacherId').val()
                },
                dataType: 'json',
                contentType: 'application/json',
                type: 'get',
                success: function (result) {
                    $('.tipsLoad').remove();
                    console.log(result);
                    if (result.count <= 0) {
                        $('#userIdTips').hide();
                    } else {
                        $('#userIdTips').show();
                    }
                },
                error: function (result) {
                    console.log(result);
                }
            })
        })
    })
</script>
<script>
</script>
</body>
</html>